package com.googlecode.futurecode.xproject.user.action;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

import com.googlecode.futurecode.xproject.types.message.user.getusers.GetUsersRequest;
import com.googlecode.futurecode.xproject.types.message.user.getusers.GetUsersResponse;
import com.googlecode.futurecode.xproject.types.message.user.login.LoginRequest;
import com.googlecode.futurecode.xproject.user.UserPortType;
import com.googlecode.futurecode.xproject.user.XpUser;
import com.googlecode.futurecode.xproject.util.DefaultXProjectAction;
import com.googlecode.futurecode.xproject.util.FunctionModel;
import com.googlecode.futurecode.xproject.util.XProjectConstants;


public class UserAction extends DefaultXProjectAction {
	private static final Logger log = Logger.getLogger(UserAction.class);
	
	private UserPortType userClient;
	private int page;
	private int rows;
	private XpUser user;
	private String loginName;
	private String password;
	private List menuList;
	
	public String getUserData() throws Exception {
		Enumeration<String> parameterNames = request.getParameterNames();
		while(parameterNames.hasMoreElements()){
			String param = parameterNames.nextElement();
			System.out.println("param:" + param + "  value:" + request.getParameter(param));
		}
		GetUsersRequest req = new GetUsersRequest();
		if(page <= 0){
			page = 1;
		}
		if(rows <= 0){
			rows = 20;
		}
		req.setPageNumber(page);
		req.setPageSize(rows);
		GetUsersResponse response = userClient.getUsers(req);
		List users = response.getUsers();

		int totalCount = response.getTotalCount();
		JSONArray jsonArray = JSONArray.fromObject(users);
		Map result = new HashMap();
		int totalPageCount = totalCount/rows;
		if(totalCount%rows > 0){
			totalPageCount++;
		}
		result.put("totalPageCount", totalPageCount);
		result.put("pageNumber", page);
		result.put("totalRecordCount", totalCount);
		result.put("rows", jsonArray);
		JSONObject jsonObjects = JSONObject.fromObject(result);
		setMessage(jsonObjects.toString());
		return SUCCESS;
	}
	
	public String login() throws Exception {
		return SUCCESS;
	}
	
	public String loginSubmit() throws Exception {
		LoginRequest loginRequest = new LoginRequest();
		loginRequest.setLoginName(loginName.trim());
		loginRequest.setPassword(password);
		try {
			user = userClient.login(loginRequest).getUser();
			request.getSession().setAttribute(XProjectConstants.CURRENT_USER, user);
			message = XProjectConstants.EMPTY_STRING;
		} catch (Exception e) {
			message = e.getMessage();
		}
		return SUCCESS;
	}

	public String main() throws Exception {
		user = (XpUser) request.getSession().getAttribute(XProjectConstants.CURRENT_USER);
		if(user == null){
			return LOGIN;
		}else{
/*			GetAllPrivilegesRequest getAllPrivilegesRequest = new GetAllPrivilegesRequest();
			getAllPrivilegesRequest.setUserId(user.getUserId() + "");
			List<Privilege> privileges = userClient.getAllPrivileges(getAllPrivilegesRequest).getPrivileges();
			List<String> pNames = new ArrayList<String>(privileges.size());
			for (int i = 0; i < privileges.size(); i++) {
				String nameEn = privileges.get(i).getNameEn();
				if(!pNames.contains(nameEn)){
					pNames.add(nameEn);
				}
			}
			request.getSession().setAttribute(AlmsConstants.CURRENT_PRIVILEGE_NAMES, pNames);*/
		}
		menuList= getMenuTree();
		return SUCCESS;
	}

	public String logout() throws Exception {
		user = (XpUser) request.getSession().getAttribute(XProjectConstants.CURRENT_USER);
        try{
			request.getSession().setAttribute(XProjectConstants.CURRENT_USER, null);
        }catch(Exception e){
        	log.error(e.getMessage(), e);
		}
		return SUCCESS;
	}
	


	public List getMenuTree() throws Exception {
		List moduleItems = new ArrayList();
		Map moduleMap = new HashMap();
		moduleMap.put("id", 1);
		moduleMap.put("text", "系统管理");
		moduleMap.put("icon", "icon-model");
		
		List functionItems = new ArrayList();
		Map functionMap = new HashMap();
		functionMap.put("id", 11);
		functionMap.put("text", "用户管理");
		functionMap.put("url", "users.action");
		functionMap.put("imageUrl", "images/69.png");
		FunctionModel model = new FunctionModel();
		model.setId(12);
		model.setText("角色管理");
		model.setUrl("");
		model.setImage("images/xxx.png");
		functionItems.add(functionMap);
		functionItems.add(model);
		moduleMap.put("children", functionItems);
		
		moduleItems.add(moduleMap);
		return moduleItems;
	}

	public UserPortType getUserClient() {
		return userClient;
	}

	public void setUserClient(UserPortType userClient) {
		this.userClient = userClient;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getRows() {
		return rows;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	public XpUser getUser() {
		return user;
	}

	public void setUser(XpUser user) {
		this.user = user;
	}

	public String getLoginName() {
		return loginName;
	}

	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public List getMenuList() {
		return menuList;
	}

}
